package com.starsoft.frame.base.web;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.starsoft.frame.base.bean.BaseAcount;
import com.starsoft.frame.base.bean.BaseUser;
import com.starsoft.frame.base.bean.UserContext;
import com.starsoft.frame.base.service.PreferenceService;
import com.starsoft.frame.base.service.UserService;
import com.starsoft.frame.core.orm.PageObject;
import com.starsoft.frame.core.web.WebUtil;


@RestController
@RequestMapping(value = "/base/user")
public class BaseUserController {

	@Autowired
	UserService userService;
	@Autowired
	PreferenceService ps;
	
	@RequestMapping("/useinfo")
	public Map<String, Object> getMyAcount(ServletRequest request){
		UserContext context = UserService.getUserContext();
		return userService.convertUser(context.getUser());
	}
	
	@RequestMapping("/updateuseinfo")
	public Map<String, Object> updateUserInfo(ServletRequest request){
		BaseUser user = UserService.getUserContext().getUser();
		Integer defautAcount = Integer.parseInt(request.getParameter("defautAcount"));
		user.setDefautAcount(defautAcount);
		user.setEmail(request.getParameter("email"));
		userService.saveUser(user);
		userService.updateUserContext();
		
		ps.saveValue("homePage", request.getParameter("homePage"), UserService.getUserContext());
		
		return WebUtil.sucesseModel("保存成功");
	}
	
	
	@RequestMapping(value = "/pageuser")
	public Map<String, Object> pageQueryUser(HttpServletRequest request){
		PageObject po = WebUtil.getPageObject(request);
		return userService.pageQueryUser(po);
	}
	
	@RequestMapping(value = "/saveuser")
	public void saveUser(BaseUser user){
		userService.saveUser(user);
	}
	
	@RequestMapping(value = "/deleteuser")
	public void deleteUser(BaseUser user){
		userService.deleteUser(user);
	}

}
